/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */
package com.nai365.nart.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nai365.nart.product.entity.Category;
import com.nai365.nart.product.entity.CategoryBrandRelation;
import com.nai365.nart.product.mapper.CategoryMapper;
import com.nai365.nart.product.service.CategoryBrandRelationService;
import com.nai365.nart.product.service.CategoryService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品三级分类
 *
 * @author baogs
 * @date 2022-07-01 11:05:46
 */
@Service
@AllArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

	CategoryBrandRelationService categoryBrandRelationService;
	@Override
	public List<Category> listWithTree(Integer type, String expensiveGoods, Long catId) {

		LambdaQueryWrapper<Category> queryWrapper = Wrappers.<Category>lambdaQuery();

		//1、查询出所有分类
		List<Category> entities = super.baseMapper.selectList(queryWrapper);

		//2、组装成父子的树形结构

		if (catId != null && !catId.equals("")) {
			List<Category> collect = entities.stream()
					.filter(d -> d.getCatId().equals(catId))
					.map((menu) -> {
						menu.setChildren(getChildrens(menu, entities));
						return menu;
					})
					.sorted((menu, menu2) -> {
						return (menu.getSort() == null ?
								0 : menu.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
					})
					.collect(Collectors.toList());
			return collect;
		}else {
			//2.1)、找到所有一级分类
			List<Category> levelMenus = entities.stream()
					.filter(e -> e.getParentCid() == 0)
					.map((menu) -> {
						menu.setChildren(getChildrens(menu, entities));

						return menu;
					})
					.sorted((menu, menu2) -> {
						return (menu.getSort() == null ?
								0 : menu.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort()); })
					.collect(Collectors.toList());
			return levelMenus;
		}

	}

	//递归查找所有菜单的子菜单
	@Override
	public List<Category> getChildrens(Category root, List<Category> all) {

		List<Category> children = all.stream().filter(categoryEntity -> {
			return categoryEntity.getParentCid().equals(root.getCatId());
		}).map(categoryEntity -> {
			//1、找到子菜单(递归)
			categoryEntity.setChildren(getChildrens(categoryEntity, all));
			return categoryEntity;
		}).sorted((menu, menu2) -> {
			//2、菜单的排序
			return (menu.getSort() == null ? 0 : menu.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
		}).collect(Collectors.toList());

		return children;

	}


	@Override
	public void removeMenuByIds(List<Long> asList) {
		//TODO 检查当前删除的菜单，是否被别的地方引用
		List<CategoryBrandRelation> categoryBrandRelation =
				categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelation>().in("catelog_id", asList));

		if (categoryBrandRelation.size() == 0) {
			//逻辑删除
			baseMapper.deleteBatchIds(asList);
		} else {
			throw new RuntimeException("该菜单下面还有属性，无法删除!");
		}
	}

}
